# Contributor: Valery Kartel <valery.kartel@gmail.com>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=rrdtool
pkgver=1.8.0
pkgrel=5
pkgdesc="Data logging and graphing application"
url="http://www.rrdtool.org"
arch="all"
license="GPL-2.0-or-later"
depends="font-sony-misc"
makedepends="libart-lgpl-dev libpng-dev freetype-dev perl-dev cairo-dev libxml2-dev
	pango-dev lua lua-dev groff autoconf automake libtool"
checkdepends="diffutils"
subpackages="$pkgname-dev $pkgname-doc perl-rrd:perl_rrd lua-rrd:lua_rrd
	$pkgname-cached $pkgname-cached-openrc:cached_openrc $pkgname-cgi $pkgname-utils librrd:libs"
source="https://github.com/oetiker/rrdtool-1.x/releases/download/v$pkgver/rrdtool-$pkgver.tar.gz
	lua-install-cmod.patch
	dont-install-in-usr-local.patch
	fix-build-date-output.patch

	rrdcached.initd
	"
options="!check" # As of 1.7.2 1/22 tests fails.

replaces="py-rrd" # Removed due to depending on EOL python 2

prepare() {
	default_prepare
	libtoolize --force && aclocal && autoconf --force && automake --add-missing
}

build() {
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--disable-nls \
		--disable-tcl \
		--disable-ruby \
		--enable-rrdcgi \
		--enable-perl-site-install \
		--enable-lua-site-install \
		--with-perl-options="INSTALLDIRS=vendor"
	make
}

check() {
	make check
}

package() {
	export INSTALLDIRS=vendor
	make DESTDIR="$pkgdir" install
	find "$pkgdir" -name '.packlist' -delete
	find "$pkgdir" -name 'perllocal.pod' -delete
}

dev() {
	default_dev
	mkdir -p "$subpkgdir"/usr/share/$pkgname
	mv "$pkgdir"/usr/share/$pkgname/examples "$subpkgdir"/usr/share/$pkgname/
}

perl_rrd() {
	depends=
	pkgdesc="Perl interface for rrdtool"
	mkdir -p "$subpkgdir"/usr/lib \
		"$pkgdir"/usr/share
	mv "$pkgdir"/usr/lib/perl* "$subpkgdir"/usr/lib/
	mv "$pkgdir"/usr/share/perl* "$subpkgdir"/usr/share/
}

lua_rrd() {
	depends=
	pkgdesc="Lua interface for rrdtool"
	mkdir -p "$subpkgdir"/usr/lib/
	mv "$pkgdir"/usr/lib/lua "$subpkgdir"/usr/lib/
}

cached() {
	depends=
	pkgdesc="RRDtool data caching daemon"
	mkdir -p "$subpkgdir"/usr/sbin \
		"$subpkgdir"/var/lib/rrdcached/db \
		"$subpkgdir"/var/lib/rrdcached/journal
	mv "$pkgdir"/usr/bin/rrdcached "$subpkgdir"/usr/sbin
}

cached_openrc() {
	depends=
	pkgdesc="RRDtool data caching daemon (OpenRC init scripts)"
	install_if="openrc ${subpkgname%-openrc}=$pkgver-r$pkgrel"

	install -Dm755 "$srcdir"/rrdcached.initd "$subpkgdir"/etc/init.d/rrdcached
}

cgi() {
	depends=
	pkgdesc="create web pages containing RRD graphs based on templates"
	mkdir -p "$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/bin/rrdcgi "$subpkgdir"/usr/bin
}

utils() {
	depends=
	pkgdesc="RRDtool standalone create,update,info utilities"
	mkdir -p "$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/bin/rrdinfo \
		"$pkgdir"/usr/bin/rrdcreate \
		"$pkgdir"/usr/bin/rrdupdate \
		"$subpkgdir"/usr/bin
}

libs() {
	depends=
	default_libs
}

sha512sums="
8ae6f94d119e8d0e1ba7f2d0738f1ba008a4880d1022f1c0c5436f662d961fceec5c42e01c241493ece3d6f55c60fd7d1d264f93e678f3cf1251201dcde027c1  rrdtool-1.8.0.tar.gz
904fb16b065d879d8cec95e1f4bf67f5a3ff29afc023e8ba2b5636ba1cf5213f24279f2e9179e8a1acb889eee68b0b6527803aad14fcae9c3b98afdbf9e1f89f  lua-install-cmod.patch
7b01d5b7c83270e0e45f3186cd0882c206173d2801678c98f1a427be1927898e602535a4cd9c808e2ab8fd393a1fb5267efae53c889c368a502a06d18cfae9aa  dont-install-in-usr-local.patch
616552f49f6a265b8e7112251277e5793d0cc2408b4ec80c2ba81a7edecba427d41b97adcff038b1ebad1aadaf9d272ebb09b1f15e03c1b4e0ef4fd7445596a8  fix-build-date-output.patch
c0c27b2c2dfa8e7ec1cb1160d2bda8d7996bbea67f4ce7779da029f583c35e5e415cf46e2a1e5cb8ed2e63d2c58a68fd7471ee6bd820db4c0f4eeeb5c252f8a3  rrdcached.initd
"
